<template>
    <div>
        <card>
            <div style="width:100%;margin-bottom: 5px;">
                <Button type="primary" style="width:100%;" @click="recordClick" ghost>
                    <h3>审批进度（点击查看详情）</h3>
                </Button>
                <span>
                <span v-if="approveRecordData.length>0">
                    <Table :data="approveRecordData" :columns="approveRecordColumns" :show-header="false" size="small" stripe></Table>
                </span>
        </span>
            </div>
            <div style="width:100%;overflow:hidden;margin-bottom: -10px;">
                <Affix :offset-top="40">
                    <div style="display:inline-block;float:left;width:49%;">
                        <Button type="primary" @click="backBtn">返回</Button>&nbsp;
                    </div>
                </Affix>
            </div>
        </card>
        <div>
            <card>
                <Tabs value="name1" type="card" :animated="true" @on-click="tabOnClick">
                    <TabPane label="审核页面" name="name1" :linkOption="{component:'_p/afs-core-business/afs-case/pages/approve/loan-audit/loanAuditDetail',isFull:true,params:{bb:'bb'},pageTitle:'审核页面'}">
                        <loan-Audit-Detail ref="loanAuditDetail" :detailParams="detailParams" @getRemarks="getRemarks"></loan-Audit-Detail>
                    </TabPane>
                    <TabPane label="影像资料" name="name2" style="max-height: 800px;" :linkOption="{component:'projects/basic/pages/image/file-audit/createFileAudit',isFull:true,params:{auditParam:this.auditParam,operate:this.operate,businessDic:this.businessDic},pageTitle:this.detailParams.contractNo}">
                        <div style="height: calc(100vh - 250px);">
                            <fileAudit ref="fileAudit" @getBaseData="getBaseData" @backBtn="backBtn" :auditParam="auditParam" :businessDic="businessDic" :operate="operate" :detailParams="detailParams"></fileAudit>
                        </div>
                    </TabPane>
                    <TabPane label="VIN码解析" name="name3">
                        <loan-Audit-Vin ref="loanAuditVin" :detailParams="detailParams"></loan-Audit-Vin>
                    </TabPane>
                    <TabPane label="见证人查询" name="name4">
                        <loan-Audit-witness ref="loanAuditWitness" :detailParams="detailParams"></loan-Audit-witness>
                    </TabPane>
                </Tabs>
            </card>
        </div>
        <Modal v-model="backToPartnersModalVisible" :title="modalTitle" :closable="false" :mask-closable='false' :width="1000" :styles="{top: '180px'}">
            <div slot="footer">
                <Button type="text" @click="backToPartnersModalVisible = false">取消</Button>
                <Button type="primary" @click="submitBackToPartners">提交</Button>
            </div>
            <div>
                <Form ref="formBack" :label-width="90" inline>
                    <FormItem label="文件类型:"><Input type="text" style="width:500px" v-model="fileType" /></FormItem>
                    <FormItem label="退回话术:"><Input type="text" style="width:500px" v-model="backWords" /></FormItem>
                    <FormItem label="问题描述:"><Input type="textarea" style="width:500px" v-model="backToPartnersReason" /></FormItem>
                </Form>
            </div>
        </Modal>
        <Modal v-model="pauseModalVisible" :title="modalTitle" :closable="false" :mask-closable='false' :width="480" :styles="{top: '180px'}">
            <div slot="footer">
                <Button type="text" @click="pauseModalVisible = false">取消</Button>
                <Button type="primary" @click="pauseSubmit">提交</Button>
            </div>
            <div>
                <Form ref="formPause" :label-width="90" inline>
                    <FormItem label="暂停原因:"><Input type="textarea" :autosize="{minRows: 3}" style="width:300px;" v-model="pauseReason" /></FormItem>
                </Form>
            </div>
        </Modal>
        <Modal v-model="backToApproveModalVisible" :title="modalTitle" :closable="false" :mask-closable='false' :width="480" :styles="{top: '180px'}">
            <div slot="footer">
                <Button type="text" @click="backToApproveModalVisible = false">取消</Button>
                <Button type="primary" @click="backToApproveSubmit">提交</Button>
            </div>
            <div>
                <Form ref="formBackToApprove" :label-width="90" inline>
                    <FormItem label="备注:"><Input type="textarea" :autosize="{minRows: 3}" style="width:300px;" v-model="backToApproveReason" /></FormItem>
                </Form>
            </div>
        </Modal>
        <Modal v-model="isFirstMortgageVisible">
            <div class="ivu-modal-body">
                <div class="ivu-modal-confirm">
                    <div class="ivu-modal-confirm-head">
                        <div class="ivu-modal-confirm-head-icon ivu-modal-confirm-head-icon-confirm">
                            <i class="ivu-icon ivu-icon-ios-help-circle"></i>
                        </div><div class="ivu-modal-confirm-head-title">请确认</div>
                    </div>
                    <div class="ivu-modal-confirm-body">
                        <div>{{"首单未抵押!"}}</div>
                    </div>
                    <div class="ivu-modal-confirm-footer">
                        <button type="button" class="ivu-btn ivu-btn-text ivu-btn-small" @click="isFirstMortgageVisible = false"><!----> <!---->取消
                        </button>
                        <button type="button" class="ivu-btn ivu-btn-primary ivu-btn-small"  @click="getFileStatus"><!----> <!---->确定
                        </button>
                    </div>
                </div>
            </div>
            <div slot="footer">
            </div>
        </Modal>
        <Modal v-model="saveSubmitModalVisible">
            <div class="ivu-modal-body">
                <div class="ivu-modal-confirm">
                    <div class="ivu-modal-confirm-head">
                        <div class="ivu-modal-confirm-head-icon ivu-modal-confirm-head-icon-confirm">
                            <i class="ivu-icon ivu-icon-ios-help-circle"></i>
                        </div><div class="ivu-modal-confirm-head-title">请确认</div>
                    </div>
                    <div class="ivu-modal-confirm-body">
                        <div>{{this.saveSubmitData+"，是否通过？"}}</div>
                    </div>
                    <div class="ivu-modal-confirm-footer">
                        <button type="button" class="ivu-btn ivu-btn-text ivu-btn-small" @click="saveSubmitModalVisible = false"><!----> <!---->取消
                        </button>
                        <button type="button" class="ivu-btn ivu-btn-primary ivu-btn-small"  @click="saveSubmit"><!----> <!---->确定
                        </button>
                    </div>
                </div>
            </div>
            <div slot="footer">
            </div>
        </Modal>
        <tortoise-fraud-push v-model="fraudFeedbackModalVisible" :fraud-type-key="tortoiseFraudTypeKey" :apply-no="detailParams.applyNo" :call-step="callStep" @onClose="fraudFeedbackSubmit"/>
        <loan-back-reason :loanBackReasonParam="detailParams" :loanBaseData="baseData" :loanBackReasonVisible="this.loanBackReasonVisible" @backBtn="backBtn" @closepop="closepop" @getFileList="getFileList"></loan-back-reason>
    </div>
</template>
<script>
    import loanAuditDetail from '../loan-audit/loanAuditDetail'
    import loanAuditVin from './loanAuditVin'
    import loanAuditWitness from './loanAuditWitness'
    import fileAudit from '@/projects/basic/pages/image/file-audit/fileAudit'
    import {
        getApproveRecord,
        auditSubmit,
        submitBackToPartners,
        saveLoanFraudInfo,
        sendCaseTortoise,
        savePause,
    } from '_p/afs-core-business/api/afs-case/loan-approve/loanAudit'
    import {
        getFileStatus,
        getPassLastDate,
        getIsFirstMortgage
    } from '_p/afs-core-business/api/afs-case/loan-approve/loanReview'
    import LoanBackReason from '../../../../../basic/pages/image/file-audit/loanBackReason'
    import TortoiseFraudPush from "../tortoise/tortoise-fraud-push";
    export default {
        name: 'loanAudit',
        components: {
            TortoiseFraudPush,
            LoanBackReason,
            loanAuditDetail,
            loanAuditVin,
            loanAuditWitness,
            fileAudit,
        },
        // props: {
        //   detailParams: {},
        // },
        data() {
            return {
                modalTitle: '',
                saveSubmitData: '',
                baseData:[],
                operate:false,
                backToPartnersModalVisible: false,
                isFirstMortgageVisible:false,
                saveSubmitModalVisible: false,
                isFirstMortgage:true,
                pauseModalVisible: false,
                loanBackReasonVisible: false,
                backToApproveModalVisible: false,
                fraudFeedbackModalVisible:false,
                recordDrop: false,
                backToApproveReason: '',
                pauseReason: '',
                backToPartnersReason: '',
                backWords: '',
                fileType: '',
                approveRecordData:[],
                allRecordData:[],
                callStep:"",
                tortoiseFraudTypeKey:"loanFakeType",
                auditParam: {
                    applyStatus: this.detailParams.applyStatus,
                    taskId: this.detailParams.taskId,
                    busiNo: this.detailParams.contractNo,
                    belongNo: this.detailParams.contractNo,
                    busiNode: 'loanApply',
                },
                businessDic:[{value:"loanApply",title:"放款审核"},
                    {value:"loanApprove",title:"内部文件"},
                    {value:"orderApply",title:"零售审批"}
                ],
                loanFraudInfo:{
                    applyNo:"",
                    stageId:"",
                    dealNo:"",
                    fraudType:"",
                    isParticipation:"",
                    fraudDetail:"",
                },
                approveRecordColumns: [
                    {
                        title: '流程节点',
                        key: 'disposeNodeName',
                        render: (h, params) => {
                            return h('span', '流程节点:' + (params.row.disposeNodeName || ''))
                        },
                    },
                    {
                        title: '处理人员',
                        key: 'disposeStaff',
                        render: (h, params) => {
                            return h('span', '处理人员:' + (params.row.disposeStaff || ''))
                        },
                    },
                    {
                        title: '处理命令',
                        key: 'approveSuggestName',
                        render: (h, params) => {
                            return h(
                                'span',
                                '处理命令:' + (params.row.approveSuggestName || '')
                            )
                        },
                    },
                    {
                        title: '处理时间',
                        key: 'approveEndTime',
                        render: (h, params) => {
                            return h('span', '处理时间:' + (params.row.approveEndTime || ''))
                        },
                    },
                    {
                        title: '处理意见',
                        key: 'approveRemark',
                        render: (h, params) => {
                            return h('span', '处理意见:' + (params.row.approveRemark || ''))
                        },
                    },
                ],
            }
        },
        beforeCreate() {
            this.detailParams = this.afs.getPageParams(this);
        },
        mounted() {
            this.init()
        },
        methods: {
            init(){
                this.refresh()
                this.getIsFirstMortgage()
            },
            getIsFirstMortgage(){
                getIsFirstMortgage(this.detailParams).then(res=>{
                    if(res.code ==='0000'){
                        this.isFirstMortgage = res.data
                        if(this.isFirstMortgage ==true){
                            this.detailParams.isFirstMortgage = 'yes'
                        } else {
                            this.detailParams.isFirstMortgage = 'no'
                        }
                    }
                })
            },
            getBaseData(val){
                this.baseData = val
            },
            refresh() {
                getApproveRecord(this.detailParams).then((res) => {
                    if (res.code === '0000') {
                        this.allRecordData = res.data.records;
                        this.setApproveRecord(this.recordDrop);
                    }
                })
            },
            tabOnClick(name) {
                if (name && name === 'name3') {
                    this.$refs.loanAuditVin.summaryBtn()
                }
            },
            getFileList(){
                this.$refs.fileAudit.getFileList();
            },
            backToPartners() {
                this.detailParams.belongNo = this.detailParams.applyNo
                this.detailParams.busiNo = this.detailParams.contractNo
                this.loanBackReasonVisible = true
            },
            submitBackToPartners() {
                submitBackToPartners(this.detailParams).then((res) => {
                    if (res.code === '0000') {
                        this.$Message.success('提交成功')
                        this.backToPartnersModalVisible = false
                        this.$emit('backGo')
                    }
                })
            },
            backToApproval() {
                this.modalTitle = '退回审批备注'
                this.backToApproveModalVisible = true
            },
            fraudFeedback() {
                this.callStep="41";
                this.fraudFeedbackModalVisible=true;
            },
            fraudFeedbackSubmit(isFlush,fraudInfo) {
                this.fraudFeedbackModalVisible=false;
                console.info("反欺诈返回数据：",fraudInfo);
                if(isFlush){
                    /**上报欺诈*/
                    sendCaseTortoise(this.detailParams.applyNo).then(res=>{
                        if(res.code==="0000"){
                            this.loanFraudInfo.applyNo=fraudInfo.applyNo;
                            this.loanFraudInfo.fraudDetail=fraudInfo.fraudDetailMsg;
                            this.loanFraudInfo.fraudType=fraudInfo.fraudTypeValue;
                            this.loanFraudInfo.isParticipation=fraudInfo.dealerJoinValue;
                            this.loanFraudInfo.stageId=this.detailParams.taskId;
                            saveLoanFraudInfo(this.loanFraudInfo).then(res=>{
                                if(res.code==="0000"){
                                    this.refresh();
                                    this.$refs.loanAuditDetail.initAllData();
                                }
                            })
                        }
                    });
                }
            },

            backBtn() {
                this.whenClose();
                window.close();
            },
            whenClose(){
                this.afs.afsEmit(this,"flushData");
            },
            pauseBtn() {
                this.modalTitle = '暂停'
                this.pauseModalVisible = true
            },
            pauseSubmit() {
                let params={};
                params.contractNo=this.detailParams.contractNo;
                params.pauseDec=this.pauseReason;
                savePause(params).then(res=>{
                    if (res.code === '0000'){
                        this.pauseReason="";
                        this.pauseModalVisible = false
                        this.backBtn();
                    }
                });
            },
            backToApproveSubmit() {
                this.$Message.success("待开发");
                this.backToApproveModalVisible=false;
                this.backBtn();
            },
            recordClick() {
                this.recordDrop = !this.recordDrop
                this.setApproveRecord(this.recordDrop);
            },
            setApproveRecord(val){
                if(!val){
                    let records=[];
                    this.allRecordData.forEach(function (v){
                        if(v.approveSuggest=="backPartners"
                            ||v.approveSuggest=="backAudit"
                            ||v.approveSuggest=="back"
                            ||v.approveSuggest=="sendBack"){
                            records.push(v);
                        }
                    })
                    this.approveRecordData=records;
                }else{
                    this.approveRecordData=this.allRecordData;
                }
            },
            getRemarks(val){
                this.detailParams.approveRemark = val;
            },
            submitBtn() {
                //保单生效日期晚于当前日期次日时，初审复审进行提示
                var insuranceStartTime = this.$refs.loanAuditDetail.insuranceInfoData.insuranceStartTime;
                var insuranceStartTimeStr = insuranceStartTime.getFullYear() + "-" + this.p(insuranceStartTime.getMonth()+1) + "-" + this.p(insuranceStartTime.getDate());
                if (insuranceStartTimeStr < this.getDay(1)){
                    this.$Modal.confirm({
                        title: '请确认',
                        content: "保险生效日期不可晚于激活日次日，是否通过？",
                        onOk: () => {
                            //进行后续校验
                            this.getFirstMortgage();
                        },
                        onCancel:() => {
                            this.$parent.back();
                        },
                    });
                }else {
                    this.getFirstMortgage();
                }
            },
            // 获取当前时间，day为number，getDay(-1):昨天的日期;getDay(0):今天的日期;getDay(1):明天的日期;【以此类推】
            getDay(day) {
                var today = new Date();
                var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
                today.setTime(targetday_milliseconds); //注意，这行是关键代码

                var tYear = today.getFullYear();
                var tMonth = today.getMonth();
                var tDate = today.getDate();
                tMonth = this.doHandleMonth(tMonth + 1);
                tDate = this.doHandleMonth(tDate);
                return tYear + "-" + tMonth + "-" + tDate;
            },
            p(s) {
                return s < 10 ? '0' + s : s
            },
            doHandleMonth(month) {
                var m = month;
                if (month.toString().length == 1) {
                    m = "0" + month;
                }
                return m;
            },
            getFirstMortgage(){
                //进行后续校验
                if(!this.isFirstMortgage){
                    this.isFirstMortgageVisible = true
                } else {
                    this.getFileStatus();
                }
            },
            getFileStatus(){
                this.detailParams.busiNo = this.detailParams.applyNo
                getFileStatus(this.detailParams).then((res) => {
                    if (res.data === true) {
                        getPassLastDate(this.detailParams).then((res) => {
                            if (res.data === true) {
                                this.saveSubmit()
                            } else {
                                this.saveSubmitData = res.data
                                this.saveSubmitModalVisible = true
                                // this.$Modal.confirm({
                                //     title: '请确认',
                                //     content: this.saveSubmitData+"，是否通过？",
                                //     onOk: () => {
                                //         this.saveSubmit();
                                //     }
                                // });
                            }
                        })
                    } else {
                        this.$Message.info('存在审核未通过的影像资料，不可提交！')
                    }
                })
            },
            saveSubmit() {
                this.saveSubmitModalVisible = false
                auditSubmit(this.detailParams).then((res) => {
                    if (res.code === '0000') {
                        this.$Message.success('提交成功')
                        // this.backToPartnersModalVisible = false
                        // this.$emit('backGo')
                        this.backBtn();
                    }
                })
            },
            closepop() {
                this.loanBackReasonVisible = false
            },
        },
    }
</script>
